#!/bin/sh
#	$NetBSD: mknative-gcc,v 1.81 2015/01/31 08:50:01 mrg Exp $
#
# Shell script for generating all the constants needed for a native
# platform build of gcc.
#

# initialise

_TMPDIR=$2
_TOP=$3
_SRC=$4
_PLATFORM=$5
_DESTDIR=$6
_TOOLDIR=$7
_VPATH=`grep VPATH ${_TMPDIR}/Makefile | sed 's,^.*=[ 	]*,,'`
_GNU_DIST=`cd ${_VPATH}; pwd`

if [ -z "$_DESTDIR" ]; then
	echo "\$_DESTDIR is empty" 2>&1
	exit 1
fi

. $_TOP/tools/gcc/mknative.common

# default to GCC 4.1 for now
_OUTDIR="$_TOP/gnu"
_OUTDIRBASE="gnu"

sanitise_includes () {
	sed \
		-e "s,-I$_DESTDIR/usr/include,,g" \
		-e "s,-I$_SRC/external/lgpl3/mpfr/dist/src,,g" \
		-e "s,-I$_SRC/external/lgpl3/mpc/dist/src,,g" \
		-e "s,-I$_SRC/external/lgpl3/gmp/lib/libgmp/arch/[a-z_0-9-]*,,g" \
		-e "s,-I$_TOOLDIR/include,,g" \
		-e "s,-I/usr/include/[^ 	]*,,"
}

##### lib/crtstuff #####

get_crtstuff () {
	_subdir="$1"
	mkdir -p $_OUTDIR/lib/$_subdir/arch

	getvars gcc/Makefile \
		INCLUDES CRTSTUFF_CFLAGS CRTSTUFF_T_CFLAGS CRTSTUFF_T_CFLAGS_S \
		tm_defines xm_file xm_defines \
		| sanitise_includes \
		| write_mk $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH.mk
}

##### lib/libg2c #####

get_libg2c () {
	mkdir -p $_OUTDIR/lib/libg2c3/arch/$MACHINE_ARCH

	write_c $_OUTDIRBASE/lib/libg2c3/arch/$MACHINE_ARCH/config.h <$_TMPDIR/$_PLATFORM/libf2c/libU77/config.h
	write_c $_OUTDIRBASE/lib/libg2c3/arch/$MACHINE_ARCH/g2c.h <$_TMPDIR/$_PLATFORM/libf2c/g2c.h

	{
		getvars $_PLATFORM/libf2c/Makefile \
			F2CEXT
		getvars $_PLATFORM/libf2c/libF77/Makefile \
			ALL_CFLAGS OBJS
		getvars $_PLATFORM/libf2c/libI77/Makefile \
			ALL_CFLAGS OBJS | sed 's,=,+=,'
		getvars $_PLATFORM/libf2c/libU77/Makefile \
			ALL_CFLAGS OBJS | sed 's,=,+=,'
	} | write_mk $_OUTDIRBASE/lib/libg2c3/arch/$MACHINE_ARCH/defs.mk
}

##### lib/libgcc #####

get_libgcc () {
	_subdir="$1"
	mkdir -p $_OUTDIR/lib/lib$_subdir/arch

	# DPBIT, FPBIT only used on mn10[23]00, we don't need them.
	# XXX we should probably grab everything Just In Case for
	# the future.

	mkdir -p $_OUTDIR/lib/lib$_subdir/arch/$MACHINE_ARCH
	cd $_TMPDIR/$_PLATFORM/libgcc
	{
		getvars $_PLATFORM/libgcc/Makefile \
			INCLUDES LIB2ADD LIB2ADDEH LIB2ADD_ST \
			LIB1ASMFUNCS LIB1ASMSRC \
			LIB2_DIVMOD_FUNCS LIB2FUNCS_ST \
			LIB2FUNCS_EXTRA \
			LIBGCC2_CFLAGS \
			SHLIB_MKMAP SHLIB_MKMAP_OPTS \
			SHLIB_MAPFILES SHLIB_NM_FLAGS
		getvars gcc/Makefile \
			NOEXCEPTION_FLAGS EXTRA_HEADERS
		getlinks config.status libgcc
	}	| sanitise_includes \
		| write_mk $_OUTDIRBASE/lib/lib$_subdir/arch/$MACHINE_ARCH/defs.mk

	if [ "${MACHINE_ARCH}" = "m68000" ]
	then
		ex <<__EOF__ $_OUTDIR/lib/lib$_subdir/arch/$MACHINE_ARCH/defs.mk
/G_LIBGCC2_CFLAGS/ s/-fPIC//
wq
__EOF__
	fi

	for f in auto-target.h; do
		write_c $_OUTDIRBASE/lib/lib$_subdir/arch/$MACHINE_ARCH/$f \
		    <$_TMPDIR/$_PLATFORM/libgcc/$f
	done
}

##### lib/libgcov #####

get_libgcov () {
	_subdir="$1"
	_mf="$2"

	mkdir -p $_OUTDIR/lib/lib$_subdir/libgcov/arch/$MACHINE_ARCH

	{
		getvars $_mf \
			LIBGCOV
	} | write_mk $_OUTDIRBASE/lib/lib$_subdir/libgcov/arch/$MACHINE_ARCH/defs.mk

	write_c $_OUTDIRBASE/lib/lib$_subdir/libgcov/arch/$MACHINE_ARCH/gcov-iov.h \
	   <$_TMPDIR/gcc/gcov-iov.h

}

##### lib/libiberty #####

get_gcc_libiberty () {
	_subdir="$1"
	_libibertydir="lib/libiberty"
	mkdir -p $_OUTDIR/$_libibertydir/arch/$MACHINE_ARCH

	getvars libiberty/Makefile \
		ALLOCA EXTRA_OFILES LIBOBJS REQUIRED_OFILES \
		| write_mk $_OUTDIRBASE/$_libibertydir/defs.mk

	write_c $_OUTDIRBASE/$_libibertydir/arch/$MACHINE_ARCH/config.h \
		<$_TMPDIR/libiberty/config.h
}

##### lib/libdecnumber #####

get_libdecnumber () {
	_subdir="$1"

	mkdir -p $_OUTDIR/usr.bin/$_subdir/arch/$MACHINE_ARCH
	write_c $_OUTDIRBASE/usr.bin/$_subdir/arch/$MACHINE_ARCH/config.h \
		<$_TMPDIR/libdecnumber/config.h
}

##### lib/libgomp #####

get_libgomp () {
	_subdir="$1"

	mkdir -p $_OUTDIR/lib/$_subdir/arch/$MACHINE_ARCH
	write_c $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/config.h \
		<$_TMPDIR/$_PLATFORM/libgomp/config.h
	write_c $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/libgomp_f.h \
		<$_TMPDIR/$_PLATFORM/libgomp/libgomp_f.h
	write_mk $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/libgomp.spec \
		<$_TMPDIR/$_PLATFORM/libgomp/libgomp.spec
	write_c $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/omp.h \
		<$_TMPDIR/$_PLATFORM/libgomp/omp.h
}

##### lib/libbacktrace #####

get_libbacktrace () {
	_subdir="$1"

	mkdir -p $_OUTDIR/lib/$_subdir/arch/$MACHINE_ARCH
	write_c $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/config.h \
		<$_TMPDIR/$_subdir/config.h
	write_c $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/backtrace-supported.h \
		<$_TMPDIR/$_subdir/backtrace-supported.h
}

##### lib/libobjc #####

get_libobjc () {
	_subdir="$1/arch/$MACHINE_ARCH"
	_options="ALL_OPT_FILES"

	mkdir -p $_OUTDIR/lib/$_subdir

	{
		if [ -n "$_options" ]; then
			getvars gcc/Makefile $_options
		fi
		getvars $_PLATFORM/libobjc/Makefile \
			ALL_CFLAGS INCLUDES OBJC_SOURCE_FILES C_SOURCE_FILES OBJC_H \
			| sed "s,$_GNU_DIST,\${GNUHOSTDIST},g"
		getlinks $_TMPDIR/$_PLATFORM/libgcc/config.status libgcc
	} | sanitise_includes \
	  | write_mk $_OUTDIRBASE/lib/$_subdir/defs.mk

	write_c $_OUTDIRBASE/lib/$_subdir/config.h \
		<$_TMPDIR/$_PLATFORM/libobjc/config.h
}

##### lib/libstdc++-v3 #####

get_libstdcxx_v3 () {
	_subdir="$1"
	_ver="$2"

	mkdir -p $_OUTDIR/lib/$_subdir/arch/$MACHINE_ARCH

	_build_headers="c++config.h cxxabi_tweaks.h gthr-posix.h gthr-single.h gthr.h"
	_headers1="c_base_headers_extra_install"
	_headers1="$_headers1 tr1_headers tr2_headers decimal_headers c_compatibility_headers_install"
	_headers1="$_headers1 debug_headers parallel_headers"
	_headers2="host_headers thread_host_headers"
	_pf_headers="profile_headers profile_impl_headers"
	_pb_headers="pb_headers1 pb_headers2 pb_headers3 pb_headers4 pb_headers5 pb_headers6 pb_headers7"

	# build files
	for h in $_build_headers; do
		write_c $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/$h \
			<$_TMPDIR/$_PLATFORM/libstdc++-v3/include/$_PLATFORM/bits/$h
	done

	write_c $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/gstdint.h \
		<$_TMPDIR/$_PLATFORM/libstdc++-v3/include/gstdint.h

	{
		# libsupc++
		getvars $_PLATFORM/libstdc++-v3/libsupc++/Makefile \
			sources | sed 's/^G_sources=/G_LIBSUPCXX_SOURCES=/'
		getvars $_PLATFORM/libstdc++-v3/libsupc++/Makefile \
			c_sources | sed 's/^G_c_sources=/G_LIBSUPCXX_C_SOURCES=/'

		# includes
		getvars $_PLATFORM/libstdc++-v3/include/Makefile \
			c_base_headers std_headers | sed -e 's#/[^ 	][^ 	]*/##g' -e 's/\${GNUHOSTDIST}//g'

		# src
		getvars $_PLATFORM/libstdc++-v3/src/Makefile \
			libstdc___la_SOURCES | sed 's/^G_libstdc___la_SOURCES=/G_SRC_SOURCES=/'
		getvars $_PLATFORM/libstdc++-v3/src/c++11/Makefile \
			libc__11convenience_la_SOURCES | sed 's/^G_libc__11convenience_la_SOURCES=/G_CPP11_SOURCES=/'
		getvars $_PLATFORM/libstdc++-v3/src/c++98/Makefile \
			libc__98convenience_la_SOURCES | sed 's/^G_libc__98convenience_la_SOURCES=/G_CPP98_SOURCES=/'
		getvars $_PLATFORM/libstdc++-v3/Makefile ATOMICITY_SRCDIR \
			BASIC_FILE_CC CLOCALE_CC CCODECVT_CC CCOLLATE_CC \
			CCTYPE_CC  CMESSAGES_CC CMONEY_CC CNUMERIC_CC CTIME_CC \
			CPU_OPT_BITS_RANDOM

		# includes
		getvars $_PLATFORM/libstdc++-v3/include/Makefile \
			backward_headers c_base_headers_extra $_headers1 $_pb_headers | \
				sed -e 's#\${GNUHOSTDIST}/libstdc++-v3/include/##g'
		getvars $_PLATFORM/libstdc++-v3/include/Makefile \
			bits_headers ext_headers $_headers2 | \
				sed -e 's#\${GNUHOSTDIST}/libstdc++-v3/include/##g' -e 's#\${GNUHOSTDIST}/libstdc++-v3/config/##g'
		getvars $_PLATFORM/libstdc++-v3/include/Makefile \
			$_pf_headers | sed -e 's#\${GNUHOSTDIST}/libstdc++-v3/include/profile/##g'
		getvars $_PLATFORM/libstdc++-v3/include/Makefile \
			BASIC_FILE_H ALLOCATOR_H CSTDIO_H CLOCALE_H CMESSAGES_H CTIME_H
		getlinks $_TMPDIR/$_PLATFORM/libgcc/config.status libgcc
	} | sanitise_includes \
	  | write_mk $_OUTDIRBASE/lib/$_subdir/arch/$MACHINE_ARCH/defs.mk
}

##### usr.bin/gcc* #####

get_gcc_bootstrap () {
	_subdir="$1"
	mkdir -p $_OUTDIR/usr.bin/$_subdir/arch/$MACHINE_ARCH
	for f in auto-host tm config; do
		write_c $_OUTDIRBASE/usr.bin/$_subdir/arch/$MACHINE_ARCH/$f.h <$_TMPDIR/gcc/$f.h
	done
}

get_gcc () {
	_subdir="$1"
	mkdir -p $_OUTDIR/usr.bin/$_subdir/arch/$MACHINE_ARCH
	mkdir -p $_OUTDIR/usr.bin/libcpp/arch/$MACHINE_ARCH
	mkdir -p $_OUTDIR/usr.bin/include/arch
	_buildname="BUILD_"
	_libcppsubdir=""
	_extravars="TM_H ALL_OPT_FILES"
	_hconfig_h=""
	_extravars2="tm_file_list build_xm_include_list"
	_extravars3="tm_p_include_list"

	{
		getvars gcc/Makefile \
			${_buildname}EARLY_SUPPORT ${_buildname}ERRORS ${_buildname}PRINT \
			${_buildname}RTL ${_buildname}SUPPORT ${_buildname}VARRAY \
			${_buildname}MD | \
		    sed -e 's#build/errors.o#build-errors.o#g' \
			-e 's#build/print-rtl.o#build-print-rtl.o#g' \
			-e 's#build/rtl.o#build-rtl.o#g' \
			-e 's#build/varray.o#build-varray.o#g' \
			-e 's#build/ggc-none.o#build-ggc-none.o#g' \
			-e 's#build/##g'
		getvars gcc/Makefile \
			ALL_CFLAGS ALL_CPPFLAGS C_AND_OBJC_OBJS C_OBJS CCCP_OBJS \
			GCC_OBJS GCOV_OBJS GXX_OBJS GTM_H PROTO_OBJS ${_extravars1} \
			INCLUDES md_file OBJC_OBJS OBJS out_file version \
			BUILD_PREFIX RTL_H RTL_BASE_H TREE_H ${_hconfig_h} BASIC_BLOCK_H GCC_H \
			GTFILES_SRCDIR GTFILES_FILES_FILES GTFILES_FILES_LANGS \
			GTFILES GTFILES_LANG_DIR_NAMES NOEXCEPTION_FLAGS \
			NATIVE_SYSTEM_HEADER_DIR \
			tm_defines host_xm_file host_xm_defines tm_p_file \
			target_cpu_default ${_extravars} ${_extravars2} \
			lang_specs_files ${_extravars3} \
			common_out_file \
				| sanitise_includes
		getvars gcc/Makefile \
			LIB2ADDEHDEP | sed 's/unwind.inc//'
		getvars gcc/Makefile \
			CXX_OBJS CXX_C_OBJS | sed 's/cp\///g'
		getvars gcc/Makefile \
			F77_OBJS | sed 's/f\///g'
		getvars libcpp/Makefile \
			libcpp_a_OBJS
		getvars gcc/Makefile \
			ENABLE_SHARED
		echo G_SHLIB_LINK="$CC -shared"
		echo G_SHLIB_MULTILIB=.
	} | write_mk $_OUTDIRBASE/usr.bin/$_subdir/arch/$MACHINE_ARCH/defs.mk

	getvars gcc/Makefile \
		EXTRA_HEADERS \
			| write_mk $_OUTDIRBASE/usr.bin/include/arch/$MACHINE_ARCH.mk

	write_c $_OUTDIRBASE/usr.bin/libcpp/arch/$MACHINE_ARCH/config.h <$_TMPDIR/libcpp/config.h
	hfiles='auto-host configargs config bconfig bversion plugin-version multilib tm'
	for f in $hfiles; do
		write_c $_OUTDIRBASE/usr.bin/$_subdir/arch/$MACHINE_ARCH/$f.h <$_TMPDIR/gcc/$f.h
		if [ "${MACHINE_ARCH}" = "powerpc" -a "${f}" = "configargs" ]
		then
			ex <<__EOF__ $_OUTDIR/usr.bin/$_subdir/arch/$MACHINE_ARCH/$f.h
/configuration_arguments/ s/$//
ya
i
#ifdef _SOFT_FLOAT
.
pu
s/";$/ -with-float=soft";/
a
#else
#endif
.
. m +1
/configure_default_options/ s/{ NULL.*$//
a
#ifdef _SOFT_FLOAT
  { "float", "soft" },
#endif
  { NULL, NULL }
};
.
wq
__EOF__
		fi
		if [ "${f}" = "configargs" ]
		then
			_srcquoted=$(echo "$_SRC" | sed 's/\//\\\//g')
			ex <<__EOF__ $_OUTDIR/usr.bin/$_subdir/arch/$MACHINE_ARCH/$f.h
/static const char configuration_arguments/ s/$_srcquoted/\/usr\/src/g
wq
__EOF__
		fi
	done

	# keep identical
	for f in all-tree.def; do
		cp $_TMPDIR/gcc/$f $_OUTDIR/usr.bin/$_subdir/arch/$MACHINE_ARCH/$f
	done

	# special transforms
	for f in gtyp-input.list; do
		sed -e 's/^.*external\/gpl3\/gcc\/dist/SRCDIR/' < $_TMPDIR/gcc/$f > $_OUTDIR/usr.bin/$_subdir/arch/$MACHINE_ARCH/$f
	done

	# special platforms
	if [ "${MACHINE_ARCH}" = "sh3el" -o "${MACHINE_ARCH}" = "sh3eb" ]; then
		write_c $_OUTDIRBASE/usr.bin/$_subdir/arch/$MACHINE_ARCH/sysroot-suffix.h <$_TMPDIR/gcc/sysroot-suffix.h
	fi
}

##### main #####

case "$1" in
# .mk and .h files for libgcc bootstrap (from host build)

libgcc*-bootstrap)
	_OUTDIR="$_TOP/external/gpl3/gcc"
	_OUTDIRBASE="external/gpl3/gcc"
	get_libgcc gcc
	get_libgcov gcc $_PLATFORM/libgcc/Makefile
	get_crtstuff crtstuff
	get_gcc_bootstrap gcc
	exit 0
	;;

libstdc++-bootstrap)
	_OUTDIR="$_TOP/external/gpl3/gcc"
	_OUTDIRBASE="external/gpl3/gcc"
	get_libstdcxx_v3 libstdc++-v3 gcc
	exit 0
	;;

gcc*)
	_OUTDIR="$_TOP/external/gpl3/gcc"
	_OUTDIRBASE="external/gpl3/gcc"
	get_gcc gcc
	get_libgcc gcc
	get_libgcov gcc $_PLATFORM/libgcc/Makefile
	get_crtstuff crtstuff
	get_gcc_libiberty gcc
	get_libobjc libobjc
	get_libstdcxx_v3 libstdc++-v3 gcc
	get_libdecnumber libdecnumber
	get_libgomp libgomp
	get_libbacktrace libbacktrace
	exit 0
	;;


*)
	echo invalid arguments;
	exit 1
	;;
esac
